<?php
include_once('services.common.php');

class System_Administrator {
	
	private  $db = null;
	private  $log = null;
	private	 $voidGet = null;

	public function System_Administrator(){
		$this->db = DataService::getInstance();
		$this->log = Logger::getRootLogger();
		$this->voidGet = array(		
			'success' => 'false',
			'rows' => ''		
		);
	}
	
	private function userIsValid(){
		session_start();
		if( isset($_SESSION['user_member']) && $_SESSION['user_member'] != '' )return true;
	}
	
	private function altaMenu($data){
	
		$id_menu = $this->db->addMenu($data);
		return $id_menu;
	
	}
	
	/**
	* @remotable
    */
	
	public function getMenuRegisters($params){
		
		if (!$this->userIsValid())
			return $this->voidGet;
		
		$start 	= $params->start;
		$limit 	= $params->limit;
		$sort 	= $params->sort;
		$dir 	= $params->dir;
		$leaf 	= $params->leaf;
		$option = $params->option;
		
		$sql	= "SELECT *, id_parent is null as is_parent FROM ver_sys_menu " ;
		if(isset($params->leaf))
			$tempy = isset($params->option)?"= $option ":"is not null ";
			$sql.= $leaf?"where id_parent $tempy":"where id_parent is null " ;
		if(!isset($params->sort))
			$sql 	.= "order by id_menu";
		
		$result = $this->db->getAll($sql,$limit,$start,$sort,$dir);
		
		return $result;
	
	}
	
	/**
	* @remotable
    */
	
	public function getModulos($params){
		
		if (!$this->userIsValid())
			return $this->voidGet;
		
		$sql	= "	SELECT 	id_modulo,
							id_modulo as mod_id_modulo,
							descripcion as mod_descripcion,
							autor as mod_autor,
							version as mod_version
					FROM 	sys_modulos" ;
		
		$result = $this->db->getAll($sql);
		
		return $result;
	
	}
	
	/**
	* @remotable
    */
	
	public function getPages($params){
	
		if (!$this->userIsValid())
			return $this->voidGet;
		
		if ($handle = opendir('.')) {
			
			$regex = "^.+\.(php)$^";
			$n = 0;
			$result = array(			
				'success' => 'true',
				'rows' => array()			
			);
			$extent = array ( 
				0 => '.',
				1 => '..',
				2 => 'api.php',
				3 => 'router.php',
				4 => 'common_js_includes.php',
				5 => 'sys_login.php',
				6 => 'sys_main_menu.php'
			);
			
			while (false !== ($file = readdir($handle))) {
				if ( !is_dir($file) && 
					 !in_array($file, $extent) &&
					 preg_match($regex, $file) ) {
					$result['rows'][$n] = array ( 
						'url' => $file
					);
					$n++;
				}
			}
			
			closedir($handle);
			
			return $result;
			
		}		
		return $this->voidGet;
	
	}
	
	/**
	* @remotable
	* @formHandler
    */
	
	public function handleFromMenu($params){
		
		if (!$this->userIsValid())
			return $this->voidGet;
				
		switch($params['action']){
		
			case 'alta':
				$this->altaMenu($params);	
				break;
			case 'baja':
			case 'modificacion':
			case 'busqueda':
				return array(		
					'success' => 'false'		
				);	
				break;
		}
			
	}
	
}

?>